package com.corpcredit.webapp.action;

import java.io.Serializable;
import java.util.Date;

import com.corpcredit.Constants;
import com.corpcredit.model.Hwxx;
import com.corpcredit.model.Jbqk;
import com.corpcredit.service.GenericManager;

public class HwxxForm extends BasePage implements Serializable {
    private GenericManager<Hwxx, Long> hwxxManager;
    private Hwxx hwxx = new Hwxx();
    private Long id;
    private Jbqk jbqk;

    public void setJbqk( Jbqk jbqk )
    {
        this.jbqk = jbqk;
    }

    public void setHwxxManager(GenericManager<Hwxx, Long> hwxxManager) {
        this.hwxxManager = hwxxManager;
    }

    public Hwxx getHwxx() {
        return hwxx;
    }

    public void setHwxx(Hwxx hwxx) {
        this.hwxx = hwxx;
    }

    public void setId(Long id) {
        this.id = id;
    }

    public String delete() {
        hwxxManager.remove(hwxx.getId());
        addMessage("hwxx.deleted");

        return "list";
    }

    public String edit() {
        // Comparison to zero (vs. null) is required with MyFaces 1.2.2, not with previous versions
        if (id != null && id != 0) {
            hwxx = hwxxManager.get(id);
        } else {
            hwxx = new Hwxx();
        }

        return "edit";
    }

    public String save() {
        boolean isNew = (hwxx.getId() == null || hwxx.getId() == 0);
        hwxx.setJbqk( jbqk );
        hwxx.setUsername(getRequest().getRemoteUser());
        hwxx.setOptdate(new Date());
        hwxx.setOperation( (isNew) ? Constants.OPERATION_ADD : Constants.OPERATION_UPDATE );
        hwxxManager.save(hwxx);

        String key = (isNew) ? "hwxx.added" : "hwxx.updated";
        addMessage(key);

        if (isNew) {
            return "list";
        } else {
        	this.id = hwxx.getId();
            return "edit";
        }
    }
} 